<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2019/3/2
  Time: 23:43
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>疯狂代码生成器</title>
    <style type="text/css">
        body{
            width: 1004px;
            margin:0 auto;
        }
        h1{
            text-align: center;
        }
        .red{
            color: red;
        }
        fieldset {
            margin-top: 20px;
            border: 1px dashed #A9A9A9;
        }
        label{
            width: 100px;
            display: inline-block;
            text-align: right;
        }
    </style>
</head>
<body>
    <h1>疯狂代码生成器QUI版（一键生成前后台CRUD代码）</h1>

    <%--数据库信息：<br>
    connectionURL: <input  id="connectionURL" type="text"><br>
    userId: <input  id="userId"   type="text"><br>
    password: <input  id="password"   type="text"><br>--%>
  <fieldset>
      <legend>1.请选择需要生成的表</legend>
      <div id="tableList">

      </div>
  </fieldset>

    <%--JAVA目录路径 :<input type="text" id="targetProject "> 如：C:\DataService\src\main\java--%>
    <fieldset>
        <legend> 2.配置包名/模块名/中文模块名</legend>
        <label >包名:</label><input type="text" id="packageName" name="packageName"> 输入："common" 即实体位置目录为 "com.entity.common" ，XML、DAO位置依次类推。<br>
        <label >英文模块名:</label><input type="text" name="moduleName" id="moduleName">  如：user <br>
        <label >中文模块名:</label><input type="text" name="moduleNameChinese" id="moduleNameChinese"> 如：用户管理  <br>
        <%--<table>
            <tr>
                <td>实体类包名:</td>
                <td><input type="text" id="entityPackage"> 如：com.entity.common</td>
            </tr>
            <tr>
                <td>Mapper.xml包名:</td>
                <td><input type="text" id="mapperPackage"> 如：resources/mapper/common</td>
            </tr>
            <tr>
                <td>Mapper接口包名:</td>
                <td><input type="text" id="interfacePackage"> 如：com.dao.common</td>
            </tr>
        </table>--%>
    </fieldset>

    <fieldset>
        <legend>3.选择列表需要展示的字段</legend>
        <div id="fieldList">
            <input  name="field" type="checkbox">userName(用户名)
            <input  name="field" type="checkbox">sex(性别)
            <input  name="field" type="checkbox">age(年龄)
        </div>
    </fieldset>

    <fieldset>
        <legend>4.选择生成需要的操作按钮</legend>
        <div id="buttonData">
            <input  name="field" type="checkbox">新增
            <input  name="field" type="checkbox">删除
            <input  name="field" type="checkbox">修改
            <input  name="field" type="checkbox">查看
            <input  name="field" type="checkbox">导出
            <%--允许批量删除： <input name="batchDelete" type="radio" checked="checked" >是 <input  name="batchDelete" type="radio"  >否--%>
            <br>
            <br>
            <span class="red">温馨提示：* 按钮对应链接地址为模块名+操作名 如 user/add</span><br>
            <br>
        </div>
    </fieldset>

    <fieldset>
        <legend>5.选择需要做为查询条件的字段</legend>
        <div id="whereData">
            <input  name="field" type="checkbox">用户姓名
            <%--条件：
            <select>
                <option>=</option>
                <option>!=	</option>
                <option>></option>
                <option><	</option>
                <option>>=	</option>
                <option><=	</option>
                <option><=	</option>
                <option>%	</option>
            </select>--%>
        </div>
    </fieldset>

    <fieldset>
        <legend>6.选择表单需要录入的数据</legend>
        <div id="formData">
            <input  name="field" type="checkbox">用户姓名
            <%--控件类型：
            <select>
                <option>文本框</option>
                <option>密码框</option>
                <option>下拉框控件</option>
                <option>单选框控件</option>
                <option>复选框控件</option>
                <option>多行文本输入控件</option>
            </select>--%>
        </div>
    </fieldset>
    <br>
    <br>
    <input id="agreed" type="checkbox">我已认真阅读相关配置，同意生成代码并且承担生成代码后导致项目运行不起来的后果。
    <button type="button" onclick="start()">点我生成代码</button>
    <br>
    <br>
    <br>
    <br>
    <script type="text/javascript" src="static/js/jquery-1.8.3.min.js"></script>
    <script type="text/javascript">
        $("#tableList,#fieldList,#formData,#whereData").html("");
        $.getJSON("/getTableInfo",function (data) {
            for( var i=0;i<data.length;i++){
                $("#tableList").append('<input name="tableList"   value="'+data[i].tableName+'" type="radio">'+data[i].tableName+'('+data[i].tableComment+')<br>');
            }
            $("input[name='tableList']").change(function () {
                $("#fieldList,#formData,#whereData").html("");
                $.getJSON("/getColumnInfo",{"tableName":$("input[name='tableList']:checked").val()},function (data) {
                    for( var i=0;i<data.length;i++){
                        $("#fieldList,#formData,#whereData").append('<input name="fieldList"  columnName="'+data[i].columnComment+'" value="'+toHump(data[i].columnName)+'" type="checkbox">'+toHump(data[i].columnName)+'('+data[i].columnComment+')<br>');
                    }
                });
            });
        });
        function start() {
            if(!$("#agreed").prop('checked')){
                alert("请同意！");
            }
            else{
                if($("#packageName").val()===""||$("#moduleName").val()===""||$("#moduleNameChinese").val()===""){
                    alert("包名 模块名 模块中文名不能为空！");
                    return;
                }
                $.post("/startGenerateCode",
                    {
                        "tableName":$("input[name='tableList']:checked").val(),
                        "packageName":$("#packageName").val(),
                        "moduleName":$("#moduleName").val(),
                        "moduleNameChinese":$("#moduleNameChinese").val(),
                        "showField":getSelectVal("fieldList"),
                        "whereField":getSelectVal("whereData"),
                        "formField":getSelectVal("formData")
                    },function (data) {
                    if(data.success){
                        alert(data.msg);
                    }
                    else{
                        alert(data.msg);
                    }
                });
            }
        }

        function getSelectVal(type){
            var data=[];
            $("#"+type+" input[name='fieldList']:checked").each(function (i) {
                var obj={};
                obj.columnName=$(this).val();
                obj.columnComment=$(this).attr("columnName");
                data.push(obj);
            });
            return JSON.stringify(data);
        }

        // 下划线转换驼峰
        function toHump(name) {
            return name.replace(/\_(\w)/g, function(all, letter){
                return letter.toUpperCase();
            });
        }
        // 驼峰转换下划线
        function toLine(name) {
            return name.replace(/([A-Z])/g,"_$1").toLowerCase();
        }
    </script>
</body>
</html>
